package com.zmn.plat.utils.bean;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;

/**
 * 类描述：文件工具类
 *
 * @author liuyong
 * @since 2021/09/08 15:28
 */
@Slf4j
public class FileUtil {

    /**
     * @param response
     * @param request
     */
    public static void downloadFile(HttpServletResponse response, HttpServletRequest request, String filePath) {
        log.info("开始下载文件={}", filePath);
        try {
            //取出文件资源
            Resource resource = new DefaultResourceLoader().getResource("classpath:" + filePath);
            String fileName = resource.getFilename();
            InputStream inStream = resource.getInputStream();
            // 设置输出的格式
            response.reset();
            response.setContentType("bin");
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            // 循环取出流中的数据
            byte[] b = new byte[2000];
            int len;
            while ((len = inStream.read(b)) > 0) {
                response.getOutputStream().write(b, 0, len);
            }
            inStream.close();
            log.info("下载完成");
        } catch (IOException e) {
            log.error("下载excel模板失败={}", e);
        }
    }

    /**
     * 获取单元格值
     *
     * @param cell
     * @return
     */
    public static String getCellValue(Cell cell) {
            String cellValue = "";
        if (cell == null) {
            return cellValue;
        }
        //如果当前单元格内容为日期类型，需要特殊处理
        String dataFormatString = cell.getCellStyle().getDataFormatString();
        if (dataFormatString.equals("m/d/yy")) {
            cellValue = new SimpleDateFormat("yyyy/MM/dd").format(cell.getDateCellValue());
            return cellValue;
        }
        //把数字当成String来读，避免出现1读成1.0的情况
        if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            cell.setCellType(Cell.CELL_TYPE_STRING);
        }
        //判断数据的类型
        switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                //数字
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            case Cell.CELL_TYPE_STRING:
                //字符串
                cellValue = String.valueOf(cell.getStringCellValue());
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                //Boolean
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                //公式
                cellValue = String.valueOf(cell.getCellFormula());
                break;
            case Cell.CELL_TYPE_BLANK:
                //空值
                cellValue = "";
                break;
            case Cell.CELL_TYPE_ERROR:
                //故障
                cellValue = null;
                break;
            default:
                cellValue = null;
                break;
        }
        return cellValue;
    }
}
